package com.dzsw.javalock;

/**
 * synchronized 锁实例
 * 说明 sendEmail 获取到的是 sendSMS 已经获取的锁
 * result :
 * T1  sendSMS()
 * T1 sendMail()
 * T2  sendSMS()
 * T2 sendMail()
 */
public class SimpleSynchronizedLock {

    public synchronized void sendSMS() throws Exception {
        System.out.println(Thread.currentThread().getName() + "  sendSMS()");
        sendEmail();
    }

    public synchronized void sendEmail() throws Exception {
        System.out.println(Thread.currentThread().getName() + " sendMail()");
    }

    public static void main(String[] args) {
        SimpleSynchronizedLock synchronizedLock = new SimpleSynchronizedLock();
        new Thread(() -> {
            try {
                synchronizedLock.sendSMS();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "T1").start();

        new Thread(() -> {
            try {
                synchronizedLock.sendSMS();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "T2").start();
    }
}
